package com.ox.head.okex.common.time;

import org.springframework.lang.NonNull;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

/**
 * @author even
 * @date 2021/2/3 0003 下午 14:07
 * @describe
 */
public class DateUtils {
    final static String YYYY_MM_DD_T_HH_MM_SSS_Z="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    /**
     * 获取时间格式
     * @param
     * @return
     * @ahtor even
     * @since
     */
    public static String timestamp(){
        return  LocalDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(YYYY_MM_DD_T_HH_MM_SSS_Z));
    }

    /**
     * 转成UTC 时间
     * @param localDateTime
     * @return
     * @ahtor even
     * @since
     */
    public static String toUtc(@NonNull LocalDateTime localDateTime){
        return localDateTime.atZone(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern(YYYY_MM_DD_T_HH_MM_SSS_Z));
    }


    /**
     * 获取一天的凌晨时间和结束时间
     *
     * @return
     */
    public static List<LocalDateTime> getStartAndEndDay() {
        List<LocalDateTime> localDateTimes = new ArrayList<>(2);
        //当天零点
        LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        //获取当天结束时间
        LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        localDateTimes.add(todayStart);
        localDateTimes.add(todayEnd);
        return localDateTimes;
    }

    /**
     * 获取某月的开始日期
     *
     * @param offset 0本月，1下个月，-1上个月，依次类推
     *               比如2019-03-28 输出2019-01-01
     * @return
     */
    public static LocalDate monthStart(int offset) {
        return LocalDate.now().plusMonths(offset).with(TemporalAdjusters.firstDayOfMonth());
    }



    /**
     * 转成北京时间
     * @param localDateTime
     * @return
     * @ahtor even
     * @since
     */
    public static LocalDateTime toUtcBeijing(LocalDateTime localDateTime){
        ZonedDateTime zonedTime = localDateTime.atZone(ZoneId.from(ZoneOffset.UTC));
        ZonedDateTime converted = zonedTime.withZoneSameInstant(ZoneOffset.ofHours(8));
        return converted.toLocalDateTime();
    }

    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    public static LocalDateTime toUtcBeijing(String time){
        return toUtcBeijing(parseStringToDateTime(time,YYYY_MM_DD_T_HH_MM_SSS_Z));
    }

}
